/**
 * @program: leetcode010
 * @author: JCM
 * @create: 2023-03-26 21:13
 * @description:
 **/
public class Main {
    public boolean isMatch(String s, String p) {
        s = " " + s;
        p = " " + p;
        char[] ss = s.toCharArray();
        char[] ps = p.toCharArray();
        int n = s.length(),m = p.length();
        boolean [][] dp = new boolean[n+1][m+1];
        dp[0][0] = true;
        for(int i =1; i <= n;i++) {
            for(int j = 1; j <= m; j++) {
                if(ps[j-1] == ss[i-1] || ps[j-1] == '.') {
                    dp[i][j] = dp[i-1][j-1];
                }else if(ps[j-1] == '*') {
                    if(ps[j-2] != ss[i-1] && ps[j-2] != '.') {
                        dp[i][j] = dp[i][j-2];
                    }else {
                        dp[i][j] = dp[i-1][j] || dp[i][j-1] || dp[i][j-2];
                    }
                }
            }
        }
        return dp[n][m];
    }
}
